package com.lottery.util;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lottery.entity.LotteryInfo;

public abstract class FastJsonUtil {

  private FastJsonUtil() {
  }

  public static String bean2String(Object obj) {
    return JSON.toJSONString(obj, SerializerFeature.QuoteFieldNames);
  }

  public static <T> T string2BaseBean(String json, Class<T> clazz) {
    return JSON.parseObject(json, clazz, Feature.DisableSpecialKeyDetect, Feature.IgnoreNotMatch);
  }

  public static <E> List<E> string2List(String json, Class<E> clazz) {
    return JSONArray.parseArray(json, clazz);
  }

  public static void main(String[] args) {
    LotteryInfo info = new LotteryInfo();
    info.setCreateTime(new Date());
    info.setHasEnd(true);
    String json = bean2String(info);

    System.out.println(json);

    LotteryInfo info2 = string2BaseBean(json, LotteryInfo.class);
    System.out.println(info2.getCreateTime());

  }
}
